"""张量的乘除法与numpy不一样"""
import torch
# print(torch.__version__)

"""3种加法  ----size必须一致"""
x1 = torch.rand(3,3)
y1 = torch.rand(3,3)
sum1 = x1 + y1                 # 方法1
sum2 =torch.empty(3,3)
torch.add(x1, y1, out = sum2)  # 方法2
# print("x1:")
# print(x1)
# print("y1:")
# print(y1)
print("sum1:")
print(sum1)
print("sum2:")
print(sum2)
y1.add_(x1)                     # 方法3
print("y1:")
print(y1)

"""3种减法  ----size必须一致"""
x2 = torch.rand(3,3)
y2 = torch.rand(3,3)
sub1 = x2 - y2                 # 方法1
sub2 = torch.empty(3,3)
torch.sub(x2, y2, out = sub2)  # 方法2
print("sub1:")
print(sub1)
print("sub2:")
print(sub2)
x2.sub_(y2)                     # 方法3
print(x2)

"""numpy切片"""
x3 = torch.rand(3,3)
print(x3[:, 1])      # 括号内先列后行
print(x3[:, :2])

"""改变张量形状 """
x4 = torch.randn(4, 4)
print(x4)
# tensor.view()操作需要保证数据元素的总数量不变
y4 = x4.view(16)    # 默认行为1
print(y4)
# -1代表自动匹配个数
z4 = x4.view(-1, 8) # -1代表自动匹配行，8代表8列
print(z4)
print(x4.size(), y4.size(), z4.size())

"""3种乘法  ----size必须一致"""
print("-----------------------------")
x5 = torch.rand(3,3)
y5 = torch.rand(3,3)
mul1 = x5 * y5                 # 方法1
mul2 = torch.empty(3,3)
torch.mul(x5, y5, out = mul2)  # 方法2
# print(x5)
print("mul1:")
print(mul1)
print("mul2:")
print(mul2)
x5.mul_(y5)                     # 方法3
print(x5)

"""3种除法  ----size必须一致"""
print("-----------------------------")
x6 = torch.rand(3,3)
y6 = torch.rand(3,3)
div1 = x6 / y6                 # 方法1
div2 = torch.empty(3,3)
torch.div(x6, y6, out = div2)  # 方法2
print("div1:")
print(div1)
print("div2:")
print(div2)
x6.div_(y6)                     # 方法3
print(x6)









